/*  
 * Copyright (c) 2025 kkaheng
 * SPDX-License-Identifier: MIT  
 */

package com.kkaheng.grid.view.cell;

import android.graphics.Canvas;

import com.kkaheng.grid.view.GridView;

import java.util.HashMap;
import java.util.Map;

public abstract class CellAdapter extends GridView.Adapter {

    private Map<Integer, Cell> cells = new HashMap<>();
    
    @Override
    public void onDraw(int column, int row, Canvas canvas, float scaleFactor) {
        getCell(getItemType(column, row))
        .draw(getData(column, row),
            canvas, column, row,
            getWidth(column), getHeight(row), scaleFactor);
    }
    
    public abstract Object getData(int column, int row);
    
    public abstract Cell onCreateCell(int type);
    
    public <T extends Cell> T getCell(int type) {
    	if (cells.containsKey(type)) {
            return (T) cells.get(type);
        }
        cells.put(type, onCreateCell(type));
        return (T) cells.get(type);
    }

}
